//
//  HQShopDetailViewController.m
//  MobileiOS
//
//  Created by 郝庆松 on 2018/5/10.
//  Copyright © 2018年 PCITC. All rights reserved.
//

#import "HQShopDetailViewController.h"

@interface HQShopDetailViewController ()
{
    __weak IBOutlet UILabel *_wNum2;
    
}
@end

@implementation HQShopDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"产品详情";
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem barButtonItemTitle:@"管理" withTitleColor:[self colorHexValue:0x647180] withFont:16 withTarget:self withSelector:@selector(rightItem)];
    
    [self requestProductDetail];
}

/**
 产品详情管理
 */
-(void)rightItem{
    if([_w_online isEqualToString:@"1"]){
        if([_w_ishot isEqualToString:@"2"]){
            NSArray *titles = @[@"  编辑",@"  取消",@"  下架",@"  删除"];
            NSArray *images = @[@"Administration_edit",@"Administration_cancel",@"Administration_Lowerframe",@"Administration_delete"];
            MLMenuView *menuView = [[MLMenuView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 100 - 10, 0, 100, 44 * 4) WithTitles:titles WithImageNames:images WithMenuViewOffsetTop:k_StatusBarAndNavigationBarHeight WithTriangleOffsetLeft:80];
            menuView.didSelectBlock = ^(NSInteger index) {
                NSLog(@"%zd",index);
                switch (index) {
                    case 0:{
                        HQShopUploadViewController *exCempleUploadVc = [[HQShopUploadViewController alloc]init];
                        exCempleUploadVc.delegate = self;
                        exCempleUploadVc.pushType = @"EDIT";
                        exCempleUploadVc.dataInfo = _currentDataInfo;
                        [self.navigationController pushViewController:exCempleUploadVc animated:YES];
                    }break;
                    case 1:{
                        //取消促销
                        [self setHot:@"1"];
                    }break;
                    case 2:{
                        UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"确认要下架此商品吗？"  preferredStyle:UIAlertControllerStyleAlert];
                        [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                            [self setonline:@"2"];
                        }]];
                        [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
                        }]];
                        [self presentViewController:alertController animated:YES completion:nil];
                    }break;
                    case 3:{
                        UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"确定要删除此商品吗？ "  preferredStyle:UIAlertControllerStyleAlert];
                        [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                            [self workdel];
                        }]];
                        [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
                        }]];
                        [self presentViewController:alertController animated:YES completion:nil];
                    }break;
                    default:
                        break;
                }
            };
            [menuView showMenuEnterAnimation:MLEnterAnimationStyleRight];
        }else{
            NSArray *titles = @[@"  编辑",@"  促销",@"  下架",@"  删除"];
            NSArray *images = @[@"Administration_edit",@"Administration_Promotion",@"Administration_Lowerframe",@"Administration_delete"];
            MLMenuView *menuView = [[MLMenuView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 100 - 10, 0, 100, 44 * 4) WithTitles:titles WithImageNames:images WithMenuViewOffsetTop:k_StatusBarAndNavigationBarHeight WithTriangleOffsetLeft:80];
            menuView.didSelectBlock = ^(NSInteger index) {
                NSLog(@"%zd",index);
                switch (index) {
                    case 0:{
                        HQShopUploadViewController *exCempleUploadVc = [[HQShopUploadViewController alloc]init];
                        exCempleUploadVc.delegate = self;
                        exCempleUploadVc.pushType = @"EDIT";
                        exCempleUploadVc.dataInfo = _currentDataInfo;
                        [self.navigationController pushViewController:exCempleUploadVc animated:YES];
                    }break;
                    case 1:{
                        //设置促销
                        [self setHot:@"2"];
                    }break;
                    case 2:{
                        UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"确认要下架此商品吗？"  preferredStyle:UIAlertControllerStyleAlert];
                        [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                            [self setonline:@"2"];
                        }]];
                        [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
                        }]];
                        [self presentViewController:alertController animated:YES completion:nil];
                    }break;
                    case 3:{
                        UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"确定要删除此商品吗？ "  preferredStyle:UIAlertControllerStyleAlert];
                        [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                            [self workdel];
                        }]];
                        [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
                        }]];
                        [self presentViewController:alertController animated:YES completion:nil];
                    }break;
                    default:
                        break;
                }
            };
            [menuView showMenuEnterAnimation:MLEnterAnimationStyleRight];
        }

    }else if([_w_online isEqualToString:@"2"]){
        if([_w_ishot isEqualToString:@"1"]){
            NSArray *titles = @[@"  编辑",@"  促销",@"  上架",@"  删除"];
            NSArray *images = @[@"Administration_edit",@"Administration_Promotion",@"Administration_Ontheshelf",@"Administration_delete"];
            MLMenuView *menuView = [[MLMenuView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 100 - 10, 0, 100, 44 * 4) WithTitles:titles WithImageNames:images WithMenuViewOffsetTop:k_StatusBarAndNavigationBarHeight WithTriangleOffsetLeft:80];
            menuView.didSelectBlock = ^(NSInteger index) {
                NSLog(@"%zd",index);
                switch (index) {
                    case 0:{
                        HQShopUploadViewController *exCempleUploadVc = [[HQShopUploadViewController alloc]init];
                        exCempleUploadVc.delegate = self;
                        exCempleUploadVc.pushType = @"EDIT";
                        exCempleUploadVc.dataInfo = _currentDataInfo;
                        [self.navigationController pushViewController:exCempleUploadVc animated:YES];
                    }break;
                    case 1:{
                        //设置促销
                        [self setHot:@"2"];
                    }break;
                    case 2:{
                        UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"请确认您的产品已入库可以上架"  preferredStyle:UIAlertControllerStyleAlert];
                        [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                            [self setonline:@"1"];
                        }]];
                        [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
                        }]];
                        [self presentViewController:alertController animated:YES completion:nil];
                    }break;
                    case 3:{
                        UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"确定要删除此商品吗？ "  preferredStyle:UIAlertControllerStyleAlert];
                        [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                            [self workdel];
                        }]];
                        [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
                        }]];
                        [self presentViewController:alertController animated:YES completion:nil];
                    }break;
                    default:
                        break;
                }
            };
            [menuView showMenuEnterAnimation:MLEnterAnimationStyleRight];
        }else{
            NSArray *titles = @[@"  编辑",@"  取消",@"  上架",@"  删除"];
            NSArray *images = @[@"Administration_edit",@"Administration_cancel",@"Administration_Ontheshelf",@"Administration_delete"];
            MLMenuView *menuView = [[MLMenuView alloc] initWithFrame:CGRectMake([UIScreen mainScreen].bounds.size.width - 100 - 10, 0, 100, 44 * 4) WithTitles:titles WithImageNames:images WithMenuViewOffsetTop:k_StatusBarAndNavigationBarHeight WithTriangleOffsetLeft:80];
            menuView.didSelectBlock = ^(NSInteger index) {
                NSLog(@"%zd",index);
                switch (index) {
                    case 0:{
                        HQShopUploadViewController *exCempleUploadVc = [[HQShopUploadViewController alloc]init];
                        exCempleUploadVc.delegate = self;
                        exCempleUploadVc.pushType = @"EDIT";
                        exCempleUploadVc.dataInfo = _currentDataInfo;
                        [self.navigationController pushViewController:exCempleUploadVc animated:YES];
                    }break;
                    case 1:{
                        //取消促销
                        [self setHot:@"1"];
                    }break;
                    case 2:{
                        UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"请确认您的产品已入库可以上架"  preferredStyle:UIAlertControllerStyleAlert];
                        [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                            [self setonline:@"1"];
                        }]];
                        [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
                        }]];
                        [self presentViewController:alertController animated:YES completion:nil];
                    }break;
                    case 3:{
                        UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"确定要删除此商品吗？ "  preferredStyle:UIAlertControllerStyleAlert];
                        [alertController addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
                            [self workdel];
                        }]];
                        [alertController addAction:[UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
                        }]];
                        [self presentViewController:alertController animated:YES completion:nil];
                    }break;
                    default:
                        break;
                }
            };
            [menuView showMenuEnterAnimation:MLEnterAnimationStyleRight];
        }
    }else{
        [self tostView:@"正在获取数据，请稍后再试..."];
    }
}

/**
 设置促销/取消促销

 @param hot 是否促销
 */
-(void)setHot:(NSString *)hot{
    [self showHudText:@""];
    [HQNetWork requetParam:[HQParam worksSetHot:[UserModelManager userID] w_id:self.dataInfo[@"w_id"] w_ishot:hot] withMethod:[HQMethod worksSetHot] suc:^(NSDictionary *sucData) {
        [self hiddnHud];
        if([sucData[kStatus]integerValue] == 1){
            if([hot isEqualToString:@"1"]){
                [self tostView:@"产品取消促销成功"];
            }else if([hot isEqualToString:@"2"]){
                [self tostView:@"产品促销成功"];
            }
            // 获取商品详情列表
            [self requestProductDetail];
        }else{
            [self tostView:sucData[kInfo]];
        }
    } fail:^{
        [self hiddenErrorHud];
    }];
}

/**
 刷新此界面
 */
- (void)reloadShopData{
    [self requestProductDetail];
}

/**
 删除产品
 */
-(void)setonline:(NSString *)online{
    [self showHudText:@""];
    [HQNetWork requetParam:[HQParam setonline:[UserModelManager userID] w_id:self.dataInfo[@"w_id"] w_online:online] withMethod:[HQMethod setonline] suc:^(NSDictionary *sucData) {
        [self hiddnHud];
        if([sucData[kStatus]integerValue] == 1){
            if([online isEqualToString:@"1"]){
                [self tostView:@"产品上架成功"];
            }else if([online isEqualToString:@"2"]){
                [self tostView:@"产品下架成功"];
            }
            // 获取商品详情列表
            [self requestProductDetail];
        }else{
            [self tostView:sucData[kInfo]];
        }
    } fail:^{
        [self hiddenErrorHud];
    }];
}

/**
 删除产品
 */
-(void)workdel{
    [self showHudText:@""];
    [HQNetWork requetParam:[HQParam workdel:[UserModelManager userID] w_id:self.dataInfo[@"w_id"]] withMethod:[HQMethod workdel] suc:^(NSDictionary *sucData) {
        [self hiddnHud];
        if([sucData[kStatus]integerValue] == 1){
            [self tostView:@"删除产品成功"];
            // 刷新商品列表界面
            [self.delegate shopDetailReloadShopData];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [self tostView:sucData[kInfo]];
        }
    } fail:^{
        [self hiddenErrorHud];
    }];
}

/**
 请求店内商品详情
 */
-(void)requestProductDetail{
    [self showHudText:@""];
    [HQNetWork requetParam:[HQParam worksFindWorksDetailsWithWID:self.dataInfo[@"w_id"]] withMethod:[HQMethod worksFindWorksDetails] suc:^(NSDictionary *sucData) {
        [self hiddnHud];
        if([sucData[kStatus]integerValue] == 1){
            NSDictionary *dic = sucData[@"data"];
            _currentDataInfo = dic;
            [_productImage setImageWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:dic[@"w_img"]]] placeholderImage:nil success:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, UIImage * _Nonnull image) {
                _productImage.image = image;
                //                _imageHegithConstraint.constant = (SCREEN_WIDTH/(image.size.width/ image.size.height));
            } failure:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, NSError * _Nonnull error) {
                
            }];
            // 是否
            _w_online = [NSString stringWithFormat:@"%@",dic[@"w_online"]];
            _w_ishot = [NSString stringWithFormat:@"%@",dic[@"w_ishot"]];
            // 产品编号
            _fengge.text = [NSString stringWithFormat:@" %@ ",dic[@"w_num"]];
            // 剩余数量
            _wNum2.text = [NSString stringWithFormat:@"剩余数量: %@",dic[@"w_kucun"]];
            // 产品风格
            [self sourceLabel:_chanpinbianhao withTitle:@"产品风格: " withData:dic[@"w_style"]];
            // 产品成分
            [self sourceLabel:_chanpinchengfen withTitle:@"产品成分: " withData:dic[@"w_element"]];
            // 生产厂家
            [self sourceLabel:_shengChanChangJia withTitle:@"生产厂家: " withData:dic[@"w_factory"]];
            // 设计理念
            [self sourceLabel:_shejilinian withTitle:@"设计理念: " withData:dic[@"w_concept"]];
            // 产品价格
            [self sourceLabel:_jiage withTitle:@"￥" withData:dic[@"w_price"] withData2:@""];
        }else{
            [self tostView:sucData[kInfo]];
        }
    } fail:^{
        [self hiddenErrorHud];
    }];
}

/**
 查看大图
 
 @param sender 查看大图
 */
- (IBAction)lookBigImage:(id)sender {
    HQBigImageViewController *bigImageVc = [[HQBigImageViewController alloc]init];
    bigImageVc.imageURL = self.dataInfo[@"w_img"];
    [self presentViewController:bigImageVc animated:YES completion:nil];
}

@end
